#include <QByteArray>
#include <QXmlAttributes>
#include <QXmlStreamReader>
#include <QString>
#include <QDebug>
#include <QDateTime>

#include "kukarsisensorguidedll_global.h"
#include "kukarsisensorguidedll.h"
#include "package/kukarsiudpserverdll_global.h"

extern "C" KUKARSISENSORGUIDEDLLSHARED_EXPORT KuKaRsiSensroGuideDLL* GetKuKaRsiSensorGuideInstance(){
    return new KuKaRsiSensorGuide();
}


KuKaRsiSensorGuide::KuKaRsiSensorGuide(){

}
KuKaRsiSensorGuide::~KuKaRsiSensorGuide(){

}

short KuKaRsiSensorGuide::KUKADataRecv(){
    int loopi = 0;
    bool DataReady = false;
    QByteArray Data;
    QXmlStreamReader mQXmlStreamReader;
    COM_KuKaDataRead(DataReady,Data);
    if(DataReady == true){
        mQXmlStreamReader.addData(Data);
        while(!mQXmlStreamReader.atEnd()){
            if(mQXmlStreamReader.isStartElement()){
                if(mQXmlStreamReader.name() == "RIst"){
                    loopi = 0;
                    foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){
                        QStringRef tempStringRef;
                        QString  tempQString;
                        tempStringRef = attribute.value();
                        tempQString = tempStringRef.toString();
                        KUKAData.KUKARIst[loopi] = tempQString.toDouble();
                        loopi++;
                    }
                }
                else if(mQXmlStreamReader.name() == "AIPos"){
                    loopi = 0;
                    foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){
                        QStringRef tempStringRef;
                        QString  tempQString;
                        tempStringRef = attribute.value();
                        tempQString = tempStringRef.toString();
                        KUKAData.KUKAAIst[loopi] = tempQString.toDouble();
                        loopi++;
                    }
                }
                else if(mQXmlStreamReader.name() == "Tor"){
                    loopi = 0;
                    foreach (const QXmlStreamAttribute & attribute, mQXmlStreamReader.attributes()){
                        QStringRef tempStringRef;
                        QString  tempQString;
                        tempStringRef = attribute.value();
                        tempQString = tempStringRef.toString();
                        KUKAData.KUKAATor[loopi]= tempQString.toDouble();
                        loopi++;
                    }
                }
                else if(mQXmlStreamReader.name() == "IPOC"){
                    QString  tempQString;
                    tempQString = (mQXmlStreamReader.readElementText());
                    KUKAData.IPOC = tempQString;
                }
            }
            mQXmlStreamReader.readNext();
        }
        mQXmlStreamReader.clear();
        return 1;
    }
    return 0;
}
short KuKaRsiSensorGuide::KUKADataSend(){
    int i = 0;
    char tempSend[64];
    QString SendString;
    QXmlStreamWriter stream(&SendString);

    memset(tempSend,'\0',strlen(tempSend));
    stream.writeEmptyElement("Sen");
        stream.writeAttribute("Type","TechSys");

        stream.writeEmptyElement("RKorr");
            sprintf(tempSend,"%f", KUKAData.KUKARInc[0]);
            stream.writeAttribute("X",tempSend);
            sprintf(tempSend,"%f", KUKAData.KUKARInc[1]);
            stream.writeAttribute("Y",tempSend);
            sprintf(tempSend,"%f", KUKAData.KUKARInc[2]);
            stream.writeAttribute("Z",tempSend);
            sprintf(tempSend,"%f", KUKAData.KUKARInc[3]);
            stream.writeAttribute("A",tempSend);
            sprintf(tempSend,"%f", KUKAData.KUKARInc[4]);
            stream.writeAttribute("B",tempSend);
            sprintf(tempSend,"%f", KUKAData.KUKARInc[5]);
            stream.writeAttribute("C",tempSend);
        stream.writeTextElement("IPOC",KUKAData.IPOC);

    stream.writeEndDocument();
    QByteArray Data;
    Data = SendString.toLocal8Bit();
    COM_KuKaDataWrite(Data);

    for(i = 0; i < 6; i++){
        KUKAData.KUKARInc[i] = 0;
    }
    return 1;
}

short KuKaRsiSensorGuide::getKUKARIst(double (&KUKARIst)[6]){
    for(int i = 0; i < 6; i++){
        KUKARIst[i] = KUKAData.KUKARIst[i];
    }
    return 1;
}
short KuKaRsiSensorGuide::getKUKAAIst(double (&KUKAAIst)[6]){
    for(int i = 0; i < 6; i++){
        KUKAAIst[i] = KUKAData.KUKAAIst[i];
    }
    return 1;
}
short KuKaRsiSensorGuide::getKUKAATor(double (&KUKAATor)[6]){
    for(int i = 0; i < 6; i++){
        KUKAATor[i] = KUKAData.KUKAATor[i];
    }
    return 1;
}

short KuKaRsiSensorGuide::setKUKARInc(double KUKARInc[6]){
    for(int i = 0; i < 6; i++){
        KUKAData.KUKARInc[i] = KUKARInc[i];
    }
    return 1;
}
